package com.lzx.makeFriend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import com.lzx.makeFriend.entity.Friend;
import com.lzx.makeFriend.entity.vo.User;
import com.lzx.makeFriend.feign.UserClient;
import com.lzx.makeFriend.mapper.FriendMapper;
import com.lzx.makeFriend.service.FriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-11-06
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {
    @Resource
    private FriendMapper friendMapper;

    @Resource
    private UserClient userClient;

    //申请添加好友
    @GlobalTransactional
    @Override
    public R applyFriend(Friend friend) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", friend.getUserId()).eq("friend_id", friend.getFriendId());
        Friend vo = friendMapper.selectOne(wrapper);
        if (vo != null) {
            vo.setCreateTime(new Date());
            friendMapper.updateById(vo);
        } else {
            friend.setCreateTime(new Date());
            friendMapper.insert(friend);

            //发送通知
            Inform inform = new Inform();
            inform.setUserId(friend.getFriendId());
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(friend.getUserId()));
            inform.setSender(friend.getUserId());
            inform.setNickname(user.getNickname());
            inform.setAvatar(user.getAvatar());
            inform.setMessage("好友申请");
            inform.setType(InformType.FRIENDAPPLY.getType());
            userClient.saveInform(inform);
        }
        return R.ok().message("申请成功");
    }

    //获取当前用户的好友申请列表
    @Override
    public R getUserFriendApplyList(Long userId) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).orderByDesc("create_time");
        List<Friend> friends = friendMapper.selectList(wrapper);
        List<Friend> list = friends.stream().peek((friend -> {
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(friend.getFriendId()));
            friend.setUser(user);
        })).collect(Collectors.toList());
        return R.ok().data("list", list);
    }

    //当前用户同意或拒绝好友申请
    @Transactional
    @Override
    public R agreeOrRejectFriendApply(Long userId, Long friendId, String type) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("friend_id", friendId);
        Friend friend = friendMapper.selectOne(wrapper);
        if (Objects.equals(type, "2")) {//拒绝好友申请
            friend.setType(type);
            friendMapper.updateById(friend);
            return R.ok().message("已拒绝");
        } else {//同意好友申请
            friend.setType(type);
            //查询朋友是否也发起了申请好友
            QueryWrapper<Friend> friendQueryWrapper = new QueryWrapper<>();
            friendQueryWrapper.eq("user_id", friendId).eq("friend_id", userId);
            Friend one = friendMapper.selectOne(friendQueryWrapper);
            if (one == null) {
                Friend vo = new Friend();
                vo.setUserId(Math.toIntExact(friendId));
                vo.setFriendId(Math.toIntExact(userId));
                vo.setIsAgree("1");
                vo.setType("1");
                vo.setCreateTime(new Date());
                friendMapper.insert(vo);
            } else {
                one.setIsAgree("1");
                one.setType("1");
                one.setCreateTime(new Date());
                friendMapper.updateById(one);
            }
            friendMapper.updateById(friend);
            return R.ok().message("已同意");
        }
    }

    //获取当前用户的好友列表
    @Override
    public R getUserFriendList(Long userId) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("type", 1);
        List<Friend> friends = friendMapper.selectList(wrapper);
        List<Friend> list = friends.stream().peek(friend -> {
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(friend.getFriendId()));
            friend.setUser(user);
        }).collect(Collectors.toList());
        return R.ok().data("list", list);
    }
}
